package controller.FleasMarketManagement;

import entity.FleasMarket;
import entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import service.FleasMarketManagement.FleasMarketService;
import util.ResponseMessage;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

@Controller
@RequestMapping("/FleasMarket")
public class FleasMarketController {

    @Autowired
    private FleasMarketService fleasMarketService;

    @CrossOrigin(allowCredentials = "true")
    @ResponseBody
    @PostMapping("/show_fleasMarket")
    public ResponseMessage show_fleasMarket(HttpSession session) {
        User Current_logged_in_user = (User) session.getAttribute("user");
        System.out.println("Current_logged_in_user = " + Current_logged_in_user);
        System.out.println((String) session.getAttribute("user_id"));
        if (Current_logged_in_user == null) {
            return new ResponseMessage(ResponseMessage.ERROR, "用户未登录");
        }
        List<LinkedHashMap<String, Object>> map = fleasMarketService.findFleasMarketItems();
        for (int i = 0; i < map.size(); i++) {
            try {
                map.get(i).put("releaseTime", String.valueOf(map.get(i).get("releaseTime")));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return new ResponseMessage(ResponseMessage.SUCCESS, "获取成功", map);
    }

    @CrossOrigin(allowCredentials = "true")
    @ResponseBody
    @PostMapping("/issue_fleasMarket")
    public ResponseMessage issue_fleasMarket(String name, String describe, Double price, HttpSession session) {
        User Current_logged_in_user = (User) session.getAttribute("user");
        System.out.println("Current_logged_in_user = " + Current_logged_in_user);
        System.out.println((String) session.getAttribute("user_id"));
        if (Current_logged_in_user == null) {
            return new ResponseMessage(ResponseMessage.ERROR, "用户未登录");
        }
        FleasMarket fleasMarket = new FleasMarket();
        fleasMarket.setName(name);
        fleasMarket.setDescribe(describe);

        // String releaseTime,获取系统当前时间
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String time = df.format(new Date());
        fleasMarket.setReleaseTime(time);// new Date()为获取当前系统时间

        fleasMarket.setPublisher(Current_logged_in_user.getUserId());
//        fleasMarket.setPublisher("15171545538");
        fleasMarket.setPrice(price);
        boolean flag = fleasMarketService.issue_fleasMarket(fleasMarket);
        if (flag) {
            int shopId = fleasMarketService.findItemsShopId(Current_logged_in_user.getUserId(), time);
//            int shopId = fleasMarketService.findItemsShopId("15171545538", time);
            return new ResponseMessage(ResponseMessage.SUCCESS, "发布成功", shopId);
        }
        return new ResponseMessage(ResponseMessage.ERROR, "系统错误");
    }


}
